package com.jackstraw.jack_base.remote_command.entity;


import com.jackstraw.jack_base.remote_command.pool.entity.SessiontConnectionConfig;
import com.jackstraw.jack_base.util.StringUtil;
import com.jackstraw.jack_base.util.entity.enums.DataBaseEnum;
import com.jackstraw.jack_base.util.exception.BadRequestException;
import lombok.Data;
import org.apache.commons.lang3.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

@Data
public class EnvProperties {
    public final static String DEFAULT_ENV = "dev";
    public final static String DEFAULT_ENVS = "dev,uat,prod";
    public final static DataBaseEnum DEFAULT_DATASOURCE = DataBaseEnum.MySQL;

    private Long myId;
    private String connName;
    private List<String> connNameS;
    private String env;
    private String envS;

    private Map<String, String> dictMap;

    //Map<环境，Map<用途，datasourceProperties>>
    private Map<String, List<DataSourceProperties>> dataSourcePropertiesMap;


    public List<String> resolveEnv(Map<String, SessiontConnectionConfig> sessiontConnectionConfigMap) {
        List<String> envList = new ArrayList<>();
        SessiontConnectionConfig sessiontConnectionConfig = sessiontConnectionConfigMap.get(SessiontConnectionConfig.getConnName(this.getConnName()));
        if (ObjectUtils.isEmpty(sessiontConnectionConfig)) {
            throw new BadRequestException("3100000004", "无法通过ConnName找到配置文件，请检查ConnName：【{}】是否正确", this.getConnName());
        }
        String ip = sessiontConnectionConfig.getIp();
        if (StringUtil.StringUtilsBylang3.isEmpty(ip)) {
            throw new BadRequestException("3100000002", "传入的connName异常");
        }
        if (ObjectUtils.isEmpty(this.myId)) {
            this.myId = Long.valueOf(sessiontConnectionConfig.getIpCode());
        }
        envList.add("My_ID".concat("=").concat(String.valueOf(this.myId)));
        envList.add("IP".concat("=").concat(ip));
        if (ObjectUtils.isNotEmpty(this.connNameS) && this.connNameS.size() > 0) {
            for (int i = 0; i < this.connNameS.size(); i++) {
                sessiontConnectionConfig = sessiontConnectionConfigMap.get(this.connNameS.get(i));
                if (ObjectUtils.isEmpty(sessiontConnectionConfig)) {
                    throw new BadRequestException("3100000004", "无法通过ConnName找到配置文件，请检查ConnName：【{}】是否正确", this.connNameS.get(i));
                }
                ip = sessiontConnectionConfig.getIpCode();
                if (StringUtil.StringUtilsBylang3.isEmpty(ip)) {
                    throw new BadRequestException("3100000003", "传入的集群connName异常");
                }
                envList.add("IP".concat(String.valueOf(i + 1)).concat("=").concat(ip));
            }
        }
        envList.add("ENV".concat("=").concat(StringUtil.StringUtilsBylang3.isEmpty(this.env) ? DEFAULT_ENV : this.env));
        envList.add("ENVS".concat("=").concat(StringUtil.StringUtilsBylang3.isEmpty(this.envS) ? DEFAULT_ENVS : this.envS));
        if (ObjectUtils.isNotEmpty(this.dataSourcePropertiesMap) && this.dataSourcePropertiesMap.size() > 0) {
            dataSourcePropertiesMap.forEach((key, value) -> {
                value.stream().forEach(dataSourceProperties -> {
                    String concat = key.concat("_").concat(dataSourceProperties.getDataBaseEnum().getDatabaseProductName())
                            .concat("_").concat(dataSourceProperties.getUsername()).concat("_").toUpperCase(Locale.ROOT);
                    envList.add(concat.concat("IP").concat("=").concat(dataSourceProperties.getIp()));
                    envList.add(concat.concat("PORT").concat("=").concat(dataSourceProperties.getPort()));
                    envList.add(concat.concat("USERNAME").concat("=").concat(dataSourceProperties.getUsername()));
                    envList.add(concat.concat("PASSWORD").concat("=").concat(dataSourceProperties.getPassword()));
                    if (StringUtil.StringUtilsBylang3.isNotEmpty(dataSourceProperties.getDbName())) {
                        envList.add(concat.concat("PASSWORD").concat("=").concat(dataSourceProperties.getDbName()));
                    }
                });
            });
        }
        if (ObjectUtils.isNotEmpty(this.getDictMap()) && this.getDictMap().size() > 0) {
            this.getDictMap().forEach((key, value) -> {
                envList.add(key.concat("=").concat(value));
            });
        }
        return envCmd(envList);
    }

    public List<String> envCmd(List<String> envList) {
        for (int i = 0; i < envList.size(); i++) {
            String evn = "echo -e " + envList.get(i);
            if (i == 0) {
                evn = evn + " >  /data/docker/.env";
            } else {
                evn = evn + " >>  /data/docker/.env";
            }
            envList.remove(i);
            envList.add(i, evn);
        }
        return envList;
    }

}
